package mysql

import (
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Mobile   string `gorm:"type:char(11);unique;comment:手机号"`
	Password string `gorm:"type:char(32);comment:密码"`
	Avatar   string `gorm:"type:varchar(300);comment:头像"`
	Status   int64  `gorm:"type:tinyint(1);default:1;comment:状态:待审核1:,2:通过:,3:不通过"`
}
type RealName struct {
	gorm.Model
	UserId   int64  `gorm:"type:int(11);not null;comment:用户id"`
	RealName string `gorm:"type:char(3);comment:姓名"`
	IdCord   string `gorm:"type:char(18);unique;comment:身份证"`
}

func (u *User) Create() error {
	return Db.Create(&u).Error
}
func (u *User) UserGetMobile(mobile string) error {
	return Db.Where("mobile = ?", mobile).Limit(1).Find(&u).Error
}
func (u *User) UpdateUserAvatar(userId int64, avatar string) error {
	return Db.Model(&User{}).Where("id = ?", userId).Update("avatar = ?", avatar).Limit(1).Find(&u).Error
}
func (r *RealName) Create() error {
	return Db.Create(&r).Error
}
func (r *RealName) RealNameInfo(userId int64, realName string) error {
	return Db.Where("user_id = ? or real_name = ?", userId, realName).Limit(1).Find(&r).Error
}
func (u *User) UserList(status, page, pageSize int64) (users []*User, err error) {
	err = Db.Where("status = ?", status).Scopes(Paginate(page, pageSize)).Limit(1).Find(&users).Error
	return
}
func Paginate(page, pageSize int64) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if page <= 0 {
			page = 1
		}
		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(int(offset)).Limit(int(pageSize))
	}
}
